<?php
/**
 * 深圳网通动力网络技术有限公司
 * Site: www.30099.cn
 * User: pengjian(szpengjian@gmail.com)
 * Date: 18-10-3
 */

class goods_tag
{

    private $db;
    private $order;
    private $customize;
    private $customize_type;

    public function __construct()
    {
        $this->db = pc_base::load_model('goods_model');
        $this->order = pc_base::load_model('goods_order_model');
        $this->customize = pc_base::load_model('goods_customize_model');
        $this->customize_type = pc_base::load_model('goods_customize_type_model');
    }

    /**
     * 首页商品列表
     * @param $data
     * @return mixed
     */
    public function lists($data)
    {
        $sql = '';
        $order = 'id ASC';
        $data['limit'] = 200;
        $search = [];
        if (!empty($_GET)) {
            foreach ($_GET as $key => $val) {
                if (!$val == 0) {
                    $search[$key] = $val;
                }
            }
            unset($search['m'], $search['c'], $search['a']);
            foreach ($search as $k => $v) {
                $sql .= " {$k} = {$v} AND";
            }
        }
        $sql = rtrim($sql, 'AND');
        $result = $this->db->select($sql, '*', $data['limit'], $order, '', 'id');
        foreach ($result as $key => $value) {
            if ($value['ship'] == 2){
                $result[$key]['reserve'] = $this->db->getGoodsNum($value['id']);
            }
            $result[$key]['url'] = 'm=goods&c=index&a=goods_info&goods_id=' . $value['id'];
        }
        return $result;
    }

    /**
     * 查询订单
     * @param $data
     * @return array
     */
    public function order($data)
    {
        $where['accept_qq'] = trim($_POST['qq']);
        $where['is_pay'] = 2;
        $sql = '';
        if (!empty($_POST['qq'])) {
            foreach ($where as $k => $v) {
                $sql .= " {$k} = {$v} AND";
            }
            $order = 'id ASC';
            $sql = rtrim($sql, 'AND');
            $result = $this->order->select($sql, '*', $data['limit'], $order, '', 'id');
        } else {
            $result = [];
        }
        return $result;
    }

    /**
     * 高级选号
     * @param $data
     */
    public function customize($data)
    {
        $result = $this->customize->select(array(), '*');
        $type = $this->customize_type->select(array(), '*');
        $data = [];
        foreach ($result as $key => $val) {
            foreach ($type as $k => $v) {
                if ($val['type'] == $v['id']) {
                    $data[$v['name']][$key]['id'] = $val['id'];
                    $data[$v['name']][$key]['name'] = $val['name'];
                    $data[$v['name']][$key]['type'] = $val['type'];
                    $data[$v['name']][$key]['sort'] = $val['sort'];
                    $data[$v['name']][$key]['data'] = $val['data'];
                }
            }
        }
        foreach ($data as $key => $val)
        {
            $data[$key] = array_values($data[$key]);
        }
        return $data;
    }
}